﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow.Domain.Models;
using WorkflowCore.Persistence.EntityFramework.Models;
using WorkflowCore.Persistence.EntityFramework.Services;

namespace Workflow.Infrastructure
{
    public class MyDbContext : WorkflowDbContext
    {
        // 配置EFCore 和连接MySQL
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var str = "server=localhost;uid=root;pwd=123456;database=Project";
            optionsBuilder.UseMySql(str, MySqlServerVersion.AutoDetect(str));
        }
        /// <summary>
        /// 访问时发生
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            // 软删除过滤
            modelBuilder.Entity<AuditModel>().HasQueryFilter(p => !p.IsDeleted);
            modelBuilder.Entity<WorkflowInstanceModel>().HasQueryFilter(p => !p.IsDeleted);
            modelBuilder.Entity<WorkflowDefinitionModel>().HasQueryFilter(p => !p.IsDeleted);
            modelBuilder.Entity<WorkflowGroupModel>().HasQueryFilter(p => !p.IsDeleted);
        }
        #region 自建表
        public DbSet<AuditModel> AuditModel { get; set; }
        public DbSet<WorkflowDefinitionModel> WorkflowDefinitionModel { get; set; }
        public DbSet<WorkflowInstanceModel> WorkflowInstanceModel { get; set; }
        public DbSet<WorkflowGroupModel> WorkflowGroupModel { get; set; }
        #endregion
        #region WorkflowCore内置表
        public DbSet<PersistedExecutionError> PersistedExecutionError { get; set; }
        public DbSet<PersistedExecutionPointer> PersistedExecutionPointer { get; set; }
        public DbSet<PersistedExtensionAttribute> PersistedExtensionAttribute { get; set; }
        public DbSet<PersistedScheduledCommand> PersistedScheduledCommand { get; set; }
        public DbSet<PersistedSubscription> PersistedSubscription { get; set; }
        public DbSet<PersistedWorkflow> PersistedWorkflow { get; set; }
        #endregion
        #region WorkflowCore内置抽象类
        protected override void ConfigureEventStorage(EntityTypeBuilder<PersistedEvent> builder)
        {
            builder.ToTable("Event");
        }

        protected override void ConfigureExecutionErrorStorage(EntityTypeBuilder<PersistedExecutionError> builder)
        {
            builder.ToTable("ExecutionError");
        }

        protected override void ConfigureExecutionPointerStorage(EntityTypeBuilder<PersistedExecutionPointer> builder)
        {
            builder.ToTable("ExecutionPointer");
        }

        protected override void ConfigureExetensionAttributeStorage(EntityTypeBuilder<PersistedExtensionAttribute> builder)
        {
            builder.ToTable("ExtensionAttribute");
        }

        protected override void ConfigureScheduledCommandStorage(EntityTypeBuilder<PersistedScheduledCommand> builder)
        {
            builder.ToTable("ScheduledCommand");
        }

        protected override void ConfigureSubscriptionStorage(EntityTypeBuilder<PersistedSubscription> builder)
        {
            builder.ToTable("Subscription");
        }

        protected override void ConfigureWorkflowStorage(EntityTypeBuilder<PersistedWorkflow> builder)
        {
            builder.ToTable("Workflow");
        }
        #endregion
    }
}
